/**
 * Created by CS on 2018/10/14.
 */
//window.onload = function () {
    var li = document.getElementById('box').getElementsByTagName('li');
    var n = 0;
//用户输入的值
    var jifen = 1000;//总积分
    var user = document.getElementById('user');
    var t;
    var ci;
    var buchang = 30, i = 0;
    var choicies = [];//保存用户选项
    var conli = document.querySelector('.con').getElementsByTagName('li');
//------------------------------------------------------------------------------------------------下一个背景变色
    function light() {
        if (n == 12) {
            n = 0;
            li[11].style.borderColor = '#fff';
        }
        li[n].style.borderColor = 'red';
        document.getElementById('diu').play();
        if (n >= 1) {
            li[n - 1].style.borderColor = '#fff';
        }
        n++;
    }


//--------------------------------------------------------------------------------停下来
    var run = function () {
        if(t){
            //console.log(t);
            clearTimeout(t);
        }
        if (i < ci) {
            document.getElementById('diu').currentTime = 0;
            t = setTimeout(run, buchang);
        }
        light();
        if (ci - i < 6 && ci - i >= 0) {
            buchang += 147;
        }
        i++;

        zhongJiang();
        //document.getElementById('diu').pause();
    };
//-------------------------------------------------------------------开始跑起来
    function start() {
        //user.readOnly = true;
        //t = setInterval('light()',50);
        //产生一个随机步数
        if(jiancejifen()){
            return false;
        }
        ci = Math.floor(60 + Math.random() * 25);
        document.getElementById('bgm').pause();
        jifen -= suoxujifen;
        showCurJifen();
        document.getElementById('btn').onclick = null;
        //获得开奖号
        jieguo = isWhere();
        //console.log(zhongjianglv);
        kongzhizj();
        run();
    }
    document.getElementById('btn').onclick = start;
//-------------------------------------------------------------------触发 双倍
    var huochebeishu = 1;
    function shuangbei() {
        if (jieguo%2 == 0){
            var chufa = Math.floor(1 + Math.random() * 3);
            console.log(chufa);
            if(chufa == 1){//s双倍
                li[isWhere()-1].style.borderColor = 'blue';
                reset();
                for(var i = 0;i < 8;i++) {
                    if(i%2 == 0){
                        setTimeout('document.body.style.backgroundColor = "blue"',500*i);
                    }else{
                        setTimeout('document.body.style.backgroundColor = "rebeccapurple"',500*i);
                    }
                }
                setTimeout(start,4000);
                zhongjianglv++;
                huochebeishu = huochebeishu * 2;
                document.getElementById('box').getElementsByTagName('h1')[0].innerHTML = '×'+huochebeishu;
                //加上跑路所需的积分
                jifen += suoxujifen;
                return true;
            }else{
                zhongjianglv++;
                return false;
            }
        }
    }
//-------------------------------------------------------------------判断现在停在哪个框框
    function isWhere() {
        //判断是否停下来
        var nnn = ci%12 + 1;
        //console.log(nnn);
        if(nnn == 13){
            return 1;
        }else{
            return nnn;
        }
    }
//-------------------------------------------------------------------控制中奖率
    var zhongjianglv = 15;
    function kongzhizj() {
        var sui = Math.floor(1 + Math.random() * 100);
        if(sui >= zhongjianglv * 10 +(choicies.length*10)){
            //结果在数组中重新产生一个结果
            while (isArr(jieguo,choicies)){
                ci = Math.floor(60 + Math.random() * 25);
                jieguo = isWhere();
            }
        }
        if(zhongjianglv == 20){
            if(suoxujifen < choicies.length*10*5){
                while (!isArr(jieguo,choicies)){
                    ci = Math.floor(60 + Math.random() * 25);
                    jieguo = isWhere();
                }
            }
        }
    }
//-----------------用户选项-----------------------

    //绑定选择点击事件
    function choice(n){
        if(isArr(n,choicies)){
            var key = isArr(n,choicies);
            choicies.splice(key-1,1);
            userLiang();
            diyaJifen(2);
        }else{
            choicies.push(n);
            userLiang();
            diyaJifen();
        }
    }
    //点亮用户选项
    function userLiang(){
        for(var x=1;x<=conli.length;x++){
            var re = isArr(x,choicies);
            if(re){
                conli[x-1].style.backgroundColor = 'orange';
            }else{
                conli[x-1].style.backgroundColor = 'transparent';
            }
        }
    }
//判断用户是否中奖
    function zhongJiang(){
        if (i==(ci+1)){//走完了
            //jieguo = isWhere();
            if(!shuangbei()){
                if (isArr(jieguo,choicies)) {
                    setTimeout("showRe(1)",500);
                    document.getElementById('box').getElementsByTagName('h1')[0].innerHTML = '×'+huochebeishu;
                    //增加积分
                    reset();
                }else{
                    setTimeout("showRe(2)",500);
                    document.getElementById('box').getElementsByTagName('h1')[0].innerHTML = '×'+huochebeishu;
                    reset();
                }
            }
        }
    }
//中奖显示窗口
    function showRe(n){
        //关闭丢丢丢声音
        document.getElementById('diu').pause();
        document.getElementById('showre').style.display  = 'block';
        if(n == 1) {
            document.getElementById('zhongjiang').play();
            jifen += huochebeishu*60*document.getElementById('beishu').value;
            var text = '恭喜中奖!号码是:'+ jieguo;
            document.getElementById('showre').children[0].innerHTML = text;
            document.getElementById('btn').onclick = start;
            zhongjianglv = 1;
            showjilu(1);
            showCurJifen();
            huochebeishu = 1;
            document.getElementById('box').getElementsByTagName('h1')[0].innerHTML = '×'+huochebeishu;
        }else if(n == 2 ){
            document.getElementById('meijiang').play();
            var text =  '继续努力!号码是:'+ jieguo;
            document.getElementById('showre').children[0].innerHTML = text;
            document.getElementById('btn').onclick = start;
            zhongjianglv++;
            showjilu(2);
            showCurJifen();
            huochebeishu = 1;
            document.getElementById('box').getElementsByTagName('h1')[0].innerHTML = '×'+huochebeishu;
        }else if(n == 3){
            document.getElementById('showre').children[0].innerHTML = '积分不足!';
        }
        closeshowtime = setTimeout("closeShow()",5000);
    }
//手动关闭显示窗
    document.getElementById('closeshow').onclick = closeShow;
    function closeShow() {
        document.getElementById('zhongjiang').pause();
        document.getElementById('meijiang').pause();
        document.getElementById('bgm').play();
        document.getElementById('showre').style.display  = 'none'
        clearTimeout(closeshowtime);
    }
//重置变量
    function reset() {
        i = 0;
        n = 0;
        ci = null;
        //choicies = [];
        buchang = 30;
        document.getElementById('zhongjiang').currentTime = 0;
        document.getElementById('meijiang').currentTime = 1;
        //jieguo = null;
        clearTimeout(t);
        userLiang();
    }
//获取元素CSS样式的函数
    function getStyle(e, styleName) {
        if (e.currentStyle) {//说明是IE
            return e.currentStyle[styleName];
        } else {
            return getComputedStyle(e)[styleName];
        }
    }
//判断数组中是否有该元素
    function isArr(v, arr) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == v) {
                return i+1;
            }
        }
        return false;
    }

    //------------------------------------------------------------------------------背景闪动
    setInterval(bgcBli,500);
    function bgcBli(){
        for ( var i = 0; i < li.length; i++){
            var hong = Math.floor(Math.random() * 256);
            var lv = Math.floor(Math.random() * 256);
            var lan = Math.floor(Math.random() * 256);
            li[i].style.backgroundColor = 'rgb('+hong+','+lv+','+lan+')';
        }
    }
/**********************************背景音乐**************************************/
    document.getElementById('bgm').autoplay = true;
    document.getElementById("diu").playbackRate = 1.5;

/**********************************积分相关**************************************/
//显示当前积分
    function showCurJifen(){
        var jf = document.getElementById('zongJifen');
        jf.value = jifen;
    }
showCurJifen();
//计算抵押积分
    function diyaJifen(v) {
        //获取抵押倍数
        if(v != 2){
            v = 1;
        }
        var beishu = document.getElementById('beishu').value;
        //计算积分
        suoxujifen = choicies.length  * beishu * 10;
        //console.log(suoxujifen);
        if (v == 1){
            if (suoxujifen > jifen){//积分不足
                showRe(3);
                beishu = 1;
                document.getElementById('beishu').value = beishu;
                choicies.pop();
            }else{//显示积分
                document.getElementById('curJifen').innerHTML = suoxujifen;
            }
        }else{
            document.getElementById('curJifen').innerHTML = suoxujifen;
        }
    }
//倍数改变时改变所需积分
    document.getElementById('beishu').onblur = diyaJifen;

//检测积分
    function jiancejifen() {
        if(jifen <= 0 || suoxujifen > jifen){
            showRe(3);
            return true;
        }else{
            return false;
        }
    }


/**********************************财神转动**************************************/
    var jiaodu = 0,kongzhi= 0;
    function caishen() {
        var caishenye = document.querySelector('.caishen');
        if (jiaodu <= 7 && jiaodu >= -7 && kongzhi == 0){
            jiaodu++;
        }else {
            jiaodu--;
        }
        if (jiaodu < -7){
            kongzhi = 0;
            jiaodu = -7;
        }
        if (jiaodu > 7 ){
            kongzhi = 1;
            jiaodu = 7;
        }
        caishenye.style.transform = "rotate(" + jiaodu + "deg)";
        //console.log(jiaodu);
        setTimeout(caishen,50);
    }
caishen();

    function btnBgc() {
        if (document.getElementById('btn').style.backgroundColor != 'yellow'){
            document.getElementById('btn').style.backgroundColor = 'yellow';
        }else{
            document.getElementById('btn').style.backgroundColor = 'red';
        }
    }
setInterval(btnBgc,500);


/**********************************游戏记录**************************************/
var lunci = 1;
    function showjilu(n) {
        if (n == 1) {
            var zhongjiangjieguo = '+' + huochebeishu*60*document.getElementById('beishu').value;
        }else{
            var zhongjiangjieguo = '未中奖';
        }
        var fa = document.getElementById('jilulist');
        var li = document.createElement('li');
        li.innerHTML = '<span>第'+ lunci + '期开奖号码:</span><b>' + jieguo + '</b><strong>' + zhongjiangjieguo + '</strong>';
        fa.appendChild(li);
        lunci++;
        document.getElementById('showjilu').scrollTop = document.getElementById('showjilu').scrollHeight;
    }
//}
